// pages/video/video.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoPath: '',
    front: 'back',
    flash: 'off',
    isPlay: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.camera']) {
          wx.authorize({
            scope: 'scope.camera',
          })
        }
        if (!res.authSetting['scope.record']) {
          wx.authorize({
            scope: 'scope.record',
          })
        }
      }
    })
    this.record = wx.createCameraContext();
    this.video = wx.createVideoContext('video');
  },
  startRecord() {
    this.record.startRecord({
      success() {
        wx.showToast({
          title: '开始录制',
        })
      }
    })
  },
  endRecord() {
    this.record.stopRecord({
      success: (res) => {
        wx.showToast({
          title: '录制结束',
        })
        let tempThumbPath = res.tempThumbPath;
        let tempVideoPath = res.tempVideoPath;
        setTimeout(() => {
          this.setData({
            videoPath: tempVideoPath
          })
        }, 300)
      }
    })
  },
  toggleFront() {
    let front = this.data.front;
    this.setData({
      front: front == 'back' ? 'front' : 'back'
    })
  },
  close() {
    this.setData({
      videoPath: ''
    })
  },
  play() {
    this.setData({
      isPlay:!this.data.isPlay
    })
    if (this.data.isPlay) {
      this.video.play();
    } else {
      this.video.pause();
    }
  }
})